﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Models.ThreadingIssues
{
    public static class Demo
    {
        public static void InvokeAll()
        {
            Action line = () =>
            {
                Console.ReadLine();
            };
            Invoke(Deadlock, line);
        }
        public static void Invoke(params Action[] actions)
        {
            foreach (var action in actions)
            {
                action();
            }
        }
        public static void RaceConditions()
        {
            var state = new StateObject();
            for (int i = 0; i < 2; i++)
            {
                Task.Run(() => new SampleTask().RaceCondition(state));
            }
        }
        public static void RaceConditionSynchronization()
        {
            var state = new StateObject();
            for (int i = 0; i < 2; i++)
            {
                Task.Run(() => new SampleTask().RaceConditionSynchronization(state));
            }
        }
        public static void Deadlock()
        {
            var s1 = new StateObject();
            var s2 = new StateObject();
            new Task(new SampleTask(s1, s2).Deadlock1).Start();
            new Task(new SampleTask(s1, s2).Deadlock2).Start();
        }
    }
}
